package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.PageResult;
import com.qk.dto.CourseDto;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public PageResult<Course> getList(CourseDto courseDto) {
        //1\分页插件
        PageHelper.startPage(courseDto.getPage(),courseDto.getPageSize());

        //2\获取列表
        List<Course> list=courseMapper.getList(courseDto);

        //3\解析分页结果
        Page<Course> pageList = (Page<Course>) list;

        return new PageResult<>(pageList.getTotal(),pageList.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        courseMapper.deleteById(id);
    }

    @Override
    public void addCourse(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.addCourse(course);
    }

    @Override
    public Course getById(Integer id) {
        return courseMapper.getById(id);
    }

    @Override
    public void updateById(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.updateById( course);
    }

    @Override
    public List<Course> getListCourse() {
        List<Course> list =courseMapper.getListCourse();
        return list;
    }

    @Override
    public List<Course> getListBySubject(Integer subject) {
        return courseMapper.getListBySubject(subject);
    }

}
